define(['./../initial', './../service/mapInstance'], function() {
    var app = angular.module('App.home');
    app.directive('monitorDataCalendar', ['mapInstance', '$compile', '$rootScope', 'autoCompleteBoxHelper',
        function(mapInstance, $compile, $rootScope, autoCompleteBoxHelper) {
            return {
                restrict: 'A',
                scope: {
                    searchOption: "=",
                    filterSearch: "=",
                    selectPrj: '='
                },
                link: function(scope, el, attrs) {
                    var panel = $('#prjFilterPanel');
                    scope.showfilterProjectPanel = function() {
                        panel.removeClass('hide').addClass('show');
                    };
                    scope.hidefilterProjectPanel = function() {
                        panel.removeClass('show').addClass('hide');
                        autoCompleteBoxHelper.closeAutoCompleteBox();
                    };

                    //接收打开或关闭filter project的消息
                    //如果有show,就hide,如果有hide就show
                    scope.$on('filterProjectClick', function(event, isShow) {
                        if (isShow === null) {
                            if (!panel.hasClass('show')) {
                                scope.showfilterProjectPanel();
                            } else {
                                scope.hidefilterProjectPanel();
                            }
                        } else if (isShow === true && !panel.hasClass('show')) {
                            scope.showfilterProjectPanel();
                        } else if (isShow === false && panel.hasClass('show')) {
                            scope.hidefilterProjectPanel();
                        }
                    });

                    scope.prjNameChanged = function() {
                        var prjName = scope.searchOption.name;
                        if (prjName !== '') {
                            var prjNameList = $rootScope.projects.filter(function(prj) {
                                return scope.filterSearch(prj);
                            }).map(function(prj) {
                                return prj.name;
                            });
                            autoCompleteBoxHelper.showAutoCompleteBox({
                                domId: "prjNameInput",
                                parentDomId: "prjFilterForm",
                                stringList: prjNameList,
                                onItemSelected: function(prjName) {
                                    scope.$apply(function() {
                                        scope.searchOption.name = prjName;
                                    });
                                },
                                onItemEntered: function(prjName) {
                                    //当用户直接回车或者鼠标点击
                                    scope.selectPrj(prjName);
                                }
                            });
                        } else {
                            autoCompleteBoxHelper.closeAutoCompleteBox();
                        }
                    };

                    scope.$on('clearFilter', function() {
                        for (var property in scope.searchOption) {
                            if (scope.searchOption.hasOwnProperty(property)) {
                                scope.searchOption[property] = "";
                                if (scope[property + 'List']) {
                                    scope[property + 'List'].forEach(function(item) {
                                        item.active = false;
                                    });
                                    scope[property + 'List'][0].active = true;
                                }
                            }
                        }
                    });

                    scope.setFilter = function(propertyName, value) {
                        for (var property in scope.searchOption) {
                            if (scope.searchOption.hasOwnProperty(property) && property === propertyName) {
                                scope.searchOption[property] = value;
                                scope[property + 'List'].forEach(function(item) {
                                    if (item.value === value) {
                                        item.active = true;
                                    } else item.active = false;
                                });
                                return;
                            }
                        }
                    };

                    scope.mPrjLevelList = ['全部', '特级', '一级', '二级', '三级'].map(function(item, index) {
                        return {
                            title: item,
                            active: index === 0 ? true : false,
                            value: index === 0 ? '' : item
                        };
                    });
                    scope.mStateList = ['全部', '桩基施工', '开挖', '报警', '新增', '地下结构', '地上结构', '后期跟踪', '停工', '已完成', '已归档', '其他'].map(function(item, index) {
                        return {
                            title: item,
                            active: index === 0 ? true : false,
                            value: index === 0 ? '' : item
                        };
                    });
                    scope.mConstructionCorpList = ['全部', '上勘院', '其他'].map(function(item, index) {
                        return {
                            title: item,
                            active: index === 0 ? true : false,
                            value: index === 0 ? '' : item
                        };
                    });
                    scope.prjTunnelList = ['全部', '单圆隧道', '双圆隧道', '车站', '明挖', '高架', '其他'].map(function(item, index) {
                        return {
                            title: item,
                            active: index === 0 ? true : false,
                            value: index === 0 ? '' : item
                        };
                    });
                },
                templateUrl: 'partials/filterProject.html'
            };
        }
    ]);

});
